package com.antdesignbackend.backend.controller;


import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.service.EnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * 预产期天数设置表
 *
 * @author 林宇杰
 * @email setday
 * @date 2020-08-27 10:41:00
 */

@Controller
@RequestMapping("/setBirthDay")
@Slf4j
public class SetBirthDayController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private EnterpriseService enterpriseService;
	@Autowired
	private AnimalNumberService animalNumberService;

	@GetMapping("/index")
	@AuthToken
	public String index(Model model) {
		Enterprise enterprise = enterpriseService.getById(getLoginEnterpriseId());
		model.addAttribute("enterprise", enterprise);
		return "setBirthDay/index";
	}

	@PostMapping("/tableSearch")
	@ResponseBody
	public ReturnVO tableSearch() {
		ReturnVO result = new ReturnVO();
		try {
			Enterprise enterprise=enterpriseService.getById(getLoginEnterpriseId());
			result.setData(enterprise.getBirthDay());
		}catch (Exception e){
			logger.error(e.getMessage());
		}
		return result;
	}
	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Enterprise enterprise, HttpServletRequest request) {
		//修改缓存
		HttpSession session = request.getSession();
		session.setAttribute("birthDay",enterprise.getBirthDay());
		enterprise.setEnterpriseId(getLoginEnterpriseId());
		ReturnVO result = new ReturnVO();
		if (enterpriseService.updateById(enterprise)) {
			result.setMessage("保存成功！");
			result.setCode("200");
		} else {
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}
}